window.frienddb = {
	insert: (friends) => {
		let friendIds = friends.map(f => f.friends.map(a => a.id)).flat();
		frienddb.selectExists(friendIds, (data) => {
			let notExists = null;
			if (data != null && data.length > 0) {
				let exists = friends.map(a => {
					a.friends = a.friends.filter(b => data.indexOf(b.id) !== -1);
					return a;
				});
				if (exists) {
					for (let gf of exists) {
						let word = gf.word;
						for (let f of gf.friends) {
							frienddb.updateExcute(word, f);
						}
					}
				}
				notExists = friends.map(a => {
					a.friends = a.friends.filter(b => data.indexOf(b.id) === -1);
					return a;
				});
			} else {
				notExists = friends;
			}
			for (let gf of notExists) {
				let word = gf.word;
				for (let f of gf.friends) {
					frienddb.insertExcute(word, f);
				}
			}
		});
	},
	insertExcute: (word, friend) => {
		let id = friend.id;
		let create_time = app.formatDate(new Date(), 'yyyy-MM-dd HH:mm:ss');
		let account = friend.account;
		let address = friend.address == null ? '' : friend.address;
		let gender = friend.gender == null ? -1 : friend.gender;
		let head_portrait = friend.headPortrait == null ? '' : friend.headPortrait;
		let nickname = friend.nickname == null ? '' : friend.nickname;
		let phone = friend.phone == null ? '' : friend.phone;
		let qr_code = friend.qrCode == null ? '' : friend.qrCode;
		let thum_head_portrait = friend.thumHeadPortrait == null ? '' : friend.thumHeadPortrait;
		let signature = friend.signature == null ? '' : friend.signature;
		let status = friend.status == null ? 1 : friend.status;
		let userId = app.getUserId();
		sql = 'insert into t_friend ' +
			'(friend_id, create_time, account, address, gender, head_portrait, nickname, phone, qr_code, thum_head_portrait, signature, status, user_id, word)' +
			'values' +
			' ("' +
			id + '","' + create_time + '", "' + account + '", "' + address + '", "' +
			gender + '", "' + head_portrait + '", "' + nickname + '", "' + phone + '", "' + qr_code + '", "' +
			thum_head_portrait + '", "' + signature + '", ' + status + ', ' + userId + ', "' + word + '") '
		db.excuteSql(sql);
	},
	delete: (params, call1, call2) => {
		sql = ' delete from t_friend where 1=1 ';
		let userId = params.userId;
		if (userId) {
			sql += ' and user_id=' + userId;
		}
		db.excuteSql(sql, call1, call2);
	},
	updateExcute: (word, friend, call) => {
		let id = friend.id;
		let address = friend.address == null ? '' : friend.address;
		let gender = friend.gender == null ? -1 : friend.gender;
		let head_portrait = friend.headPortrait == null ? '' : friend.headPortrait;
		let nickname = friend.nickname == null ? '' : friend.nickname;
		let phone = friend.phone == null ? '' : friend.phone;
		let qr_code = friend.qrCode == null ? '' : friend.qrCode;
		let thum_head_portrait = friend.thumHeadPortrait == null ? '' : friend.thumHeadPortrait;
		let signature = friend.signature == null ? '' : friend.signature;
		let status = friend.status == null ? 1 : friend.status;
		let userId = app.getUserId();
		sql = 'update t_friend set ' +
			' address="' + address + '", gender="' + gender + '", head_portrait="' + head_portrait + '", nickname="' +
			nickname + '", phone="' + phone + '", qr_code="' + qr_code + '", thum_head_portrait="' +
			thum_head_portrait + '", signature="' + signature + '", status="' + status + '", word="' + word + '"' +
			' where friend_id=' + id + ' and user_id=' + userId
		db.excuteSql(sql, call);
	},
	selectExists(friendIds, call) {
		sql = 'select id from t_friend where 1=1 and user_id='+ app.getUserId() +' and friend_id in ('
		for (var i = 0; i < friendIds.length; i++) {
			let id = friendIds[i];
			sql += '"' + id + '"';
			if (i != friendIds.length - 1) {
				sql += ','
			}
		}
		sql += ')';
		db.selectSQL(sql, (data) => {
			if (data && typeof call === 'function') {
				call(data);
			}
		})
	},
	select(params, call) {
		sql = 'select * from t_friend where 1=1 ';
		let userId = params.userId;
		if (userId) {
			sql += ' and user_id=' + userId;
		}
		db.selectSQL(sql, call);
	}
}
